home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / DirectSound / AdjustSound / adjustsound.cpp next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  24.9 KB  |  680 lines

  1. //----------------------------------------------------------------------------
  2. // File: AdjustSound.cpp
  3. //
  4. // Desc: AdjustSound sample sample shows how to load and play a wave file using
  5. //       a DirectSound buffer and adjust its focus, frequency, pan, and volume.
  6. //
  7. // Copyright (c) Microsoft Corp. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #define STRICT
  10. #include "dxstdafx.h"
  11. #include <commdlg.h>
  12. #include "resource.h"
  13.  
  14.  
  15.  
  16.  
  17. //-----------------------------------------------------------------------------
  18. // Function-prototypes
  19. //-----------------------------------------------------------------------------
  20. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg,  WPARAM wParam, LPARAM lParam );
  21.  
  22. VOID    OnInitDialog( HWND hDlg );
  23. VOID    OnTimer( HWND hDlg );
  24.  
  25. VOID    OnOpenSoundFile( HWND hDlg );
  26. VOID    ValidateWaveFile( HWND hDlg, TCHAR* strFileName );
  27.  
  28. HRESULT OnPlaySound( HWND hDlg );
  29. HRESULT CreateAndFillBuffer( HWND hDlg, DWORD dwCreationFlags );
  30.  
  31. VOID    UpdateBehaviorText( HWND hDlg );
  32. VOID    OnSliderChanged( HWND hDlg );
  33. VOID    SetBufferOptions( LONG lFrequency, LONG lPan, LONG lVolume );
  34. VOID    EnablePlayUI( HWND hDlg, BOOL bEnable );
  35. VOID    SetSlidersPos( HWND hDlg, LONG lFreqSlider, LONG lPanSlider, LONG lVolumeSlider );
  36.  
  37.  
  38.  
  39.  
  40.  
  41. //-----------------------------------------------------------------------------
  42. // Defines, constants, and global variables
  43. //-----------------------------------------------------------------------------
  44. TCHAR          m_strWaveFileName[MAX_PATH];
  45. CSoundManager* g_pSoundManager = NULL;
  46. DWORD          g_dwLastValidFreq = 0;
  47. CSound*        g_pSound = NULL;
  48.  
  49.  
  50.  
  51.  
  52. //-----------------------------------------------------------------------------
  53. // Name: WinMain()
  54. // Desc: Entry point for the application.  Since we use a simple dialog for 
  55. //       user interaction we don't need to pump messages.
  56. //-----------------------------------------------------------------------------
  57. INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, INT nCmdShow )
  58. {
  59.     // Init the common control dll 
  60.     InitCommonControls();
  61.  
  62.     // Display the main dialog box.
  63.     DialogBox( hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDlgProc );
  64.  
  65.     return TRUE;
  66. }
  67.  
  68.  
  69.  
  70.  
  71. //-----------------------------------------------------------------------------
  72. // Name: MainDlgProc()
  73. // Desc: Handles dialog messages
  74. //-----------------------------------------------------------------------------
  75. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  76. {
  77.     HRESULT hr;
  78.  
  79.     switch( msg ) 
  80.     {
  81.         case WM_COMMAND:
  82.             switch( LOWORD(wParam) )
  83.             {
  84.                 case IDCANCEL:
  85.                     EndDialog( hDlg, IDCANCEL );
  86.                     break;
  87.  
  88.                 case IDC_SOUNDFILE:
  89.                     OnOpenSoundFile( hDlg );
  90.                     break;
  91.  
  92.                 case IDC_PLAY:
  93.                     if( FAILED( hr = OnPlaySound( hDlg ) ) )
  94.                     {
  95.                         DXTRACE_ERR_MSGBOX( TEXT("OnPlaySound"), hr );
  96.                         MessageBox( hDlg, L"Error playing DirectSound buffer."
  97.                                     L"Sample will now exit.", L"DirectSound Sample", 
  98.                                     MB_OK | MB_ICONERROR );
  99.                         EndDialog( hDlg, IDABORT );
  100.                     }
  101.  
  102.                     break;
  103.  
  104.                 case IDC_STOP:
  105.                     if( g_pSound )
  106.                     {
  107.                         g_pSound->Stop();
  108.                         g_pSound->Reset();
  109.                     }
  110.                     break;
  111.  
  112.                 case IDC_MIX_DEFAULT:
  113.                 case IDC_MIX_HARDWARE:
  114.                 case IDC_MIX_SOFTWARE:
  115.                 case IDC_FOCUS_GLOBAL:
  116.                 case IDC_FOCUS_STICKY:
  117.                 case IDC_FOCUS_NORMAL:
  118.                     UpdateBehaviorText( hDlg );
  119.                     break;
  120.  
  121.                 default:
  122.                     return FALSE; // Didn't handle message
  123.             }
  124.             break;
  125.  
  126.         case WM_TIMER:
  127.             OnTimer( hDlg );
  128.             break;
  129.  
  130.         case WM_INITDIALOG:
  131.             OnInitDialog( hDlg );
  132.             break;
  133.  
  134.         case WM_NOTIFY:
  135.             OnSliderChanged( hDlg );
  136.             break;
  137.  
  138.         case WM_DESTROY:
  139.             // Cleanup everything
  140.             KillTimer( hDlg, 1 );    
  141.             SAFE_DELETE( g_pSound );
  142.             SAFE_DELETE( g_pSoundManager );
  143.             break; 
  144.  
  145.         default:
  146.             return FALSE; // Didn't handle message
  147.     }
  148.  
  149.     return TRUE; // Handled message
  150. }
  151.  
  152.  
  153.  
  154.  
  155. //-----------------------------------------------------------------------------
  156. // Name: OnInitDialog()
  157. // Desc: Initializes the dialogs (sets up UI controls, etc.)
  158. //-----------------------------------------------------------------------------
  159. VOID OnInitDialog( HWND hDlg )
  160. {
  161.     HRESULT hr;
  162.  
  163.     // Load the icon
  164. #ifdef _WIN64
  165.     HINSTANCE hInst = (HINSTANCE) GetWindowLongPtr( hDlg, GWLP_HINSTANCE );
  166. #else
  167.     HINSTANCE hInst = (HINSTANCE) GetWindowLong( hDlg, GWL_HINSTANCE );
  168. #endif
  169.     HICON hIcon = LoadIcon( hInst, MAKEINTRESOURCE( IDR_MAINFRAME ) );
  170.  
  171.     // Create a static IDirectSound in the CSound class.  
  172.     // Set coop level to DSSCL_PRIORITY, and set primary buffer 
  173.     // format to stereo, 22kHz and 16-bit output.
  174.     g_pSoundManager = new CSoundManager();
  175.     if( NULL == g_pSoundManager )
  176.     {
  177.         DXTRACE_ERR_MSGBOX( TEXT("Initialize"), E_OUTOFMEMORY );
  178.         EndDialog( hDlg, IDABORT );
  179.         return;
  180.     }
  181.  
  182.     if( FAILED( hr = g_pSoundManager->Initialize( hDlg, DSSCL_PRIORITY ) ) )
  183.     {
  184.         DXTRACE_ERR_MSGBOX( TEXT("Initialize"), hr );
  185.         MessageBox( hDlg, L"Error initializing DirectSound.  Sample will now exit.", 
  186.                           L"DirectSound Sample", MB_OK | MB_ICONERROR );
  187.         EndDialog( hDlg, IDABORT );
  188.         return;
  189.     }
  190.     
  191.     if( FAILED( hr = g_pSoundManager->SetPrimaryBufferFormat( 2, 22050, 16 ) ) )
  192.     {
  193.         DXTRACE_ERR_MSGBOX( TEXT("SetPrimaryBufferFormat"), hr );
  194.         MessageBox( hDlg, L"Error initializing DirectSound.  Sample will now exit.", 
  195.                           L"DirectSound Sample", MB_OK | MB_ICONERROR );
  196.         EndDialog( hDlg, IDABORT );
  197.         return;
  198.     }
  199.  
  200.     // Set the icon for this dialog.
  201.     PostMessage( hDlg, WM_SETICON, ICON_BIG,   (LPARAM) hIcon );  // Set big icon
  202.     PostMessage( hDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIcon );  // Set small icon
  203.  
  204.     // Create a timer, so we can check for when the soundbuffer is stopped
  205.     SetTimer( hDlg, 0, 250, NULL );
  206.  
  207.     // Get handles to dialog items
  208.     HWND hFreqSlider    = GetDlgItem( hDlg, IDC_FREQUENCY_SLIDER );
  209.     HWND hPanSlider     = GetDlgItem( hDlg, IDC_PAN_SLIDER );
  210.     HWND hVolumeSlider  = GetDlgItem( hDlg, IDC_VOLUME_SLIDER );
  211.  
  212.     // Set the focus to normal by default
  213.     CheckRadioButton( hDlg, IDC_FOCUS_NORMAL, IDC_FOCUS_NORMAL, IDC_FOCUS_NORMAL );
  214.  
  215.     // Set the buffer mixing to default 
  216.     CheckRadioButton( hDlg, IDC_MIX_DEFAULT, IDC_MIX_SOFTWARE, IDC_MIX_DEFAULT );
  217.  
  218.     // Set the range and position of the freq slider from 
  219.     // DSBFREQUENCY_MIN and DSBFREQUENCY_MAX are DirectSound constants
  220.     PostMessage( hFreqSlider, TBM_SETRANGEMAX, TRUE, DSBFREQUENCY_MAX );
  221.     PostMessage( hFreqSlider, TBM_SETRANGEMIN, TRUE, DSBFREQUENCY_MIN );
  222.  
  223.     // Set the range and position of the pan slider from 
  224.     PostMessage( hPanSlider, TBM_SETRANGEMAX, TRUE, ( 10000L/500L) );
  225.     PostMessage( hPanSlider, TBM_SETRANGEMIN, TRUE, (-10000L/500L) );
  226.  
  227.     // Set the range and position of the volume slider 
  228.     PostMessage( hVolumeSlider, TBM_SETRANGEMAX, TRUE, 0L );
  229.     PostMessage( hVolumeSlider, TBM_SETRANGEMIN, TRUE, (-5000L/100L) );
  230.  
  231.     // Set the position of the sliders
  232.     SetSlidersPos( hDlg, DSBFREQUENCY_MIN, 0, 0 );
  233.  
  234.     // Set the UI controls
  235.     SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  236.     SetDlgItemText( hDlg, IDC_STATUS, TEXT("No file loaded.") );
  237.     UpdateBehaviorText( hDlg );
  238. }
  239.  
  240.  
  241.  
  242.  
  243. //-----------------------------------------------------------------------------
  244. // Name: OnOpenSoundFile()
  245. // Desc: Called when the user requests to open a sound file
  246. //-----------------------------------------------------------------------------
  247. VOID OnOpenSoundFile( HWND hDlg ) 
  248. {
  249.     static WCHAR strFileName[MAX_PATH] = TEXT("");
  250.     static WCHAR strPath[MAX_PATH] = TEXT("");
  251.  
  252.     // Setup the OPENFILENAME structure
  253.     OPENFILENAME ofn = { sizeof(OPENFILENAME), hDlg, NULL,
  254.                          TEXT("Wave Files\0*.wav\0All Files\0*.*\0\0"), NULL,
  255.                          0, 1, strFileName, MAX_PATH, NULL, 0, strPath,
  256.                          TEXT("Open Sound File"),
  257.                          OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, 0, 0,
  258.                          TEXT(".wav"), 0, NULL, NULL };
  259.  
  260.     // Get the default media path (something like C:\WINDOWS\MEDIA)
  261.     if( '\0' == strPath[0] )
  262.     {
  263.         if( GetWindowsDirectory( strPath, MAX_PATH ) != 0 )
  264.         {
  265.             if( wcscmp( &strPath[wcslen(strPath)], L"\\" ) )
  266.                 wcscat( strPath, L"\\" );
  267.             wcscat( strPath, TEXT("MEDIA") );
  268.         }
  269.     }
  270.  
  271.     // Update the UI controls to show the sound as loading a file
  272.     EnableWindow( GetDlgItem( hDlg, IDC_PLAY ), FALSE );
  273.     EnableWindow( GetDlgItem( hDlg, IDC_STOP ), FALSE );
  274.     SetDlgItemText( hDlg, IDC_STATUS, TEXT("Loading file...") );
  275.  
  276.     if( g_pSound )
  277.     {
  278.         g_pSound->Stop();
  279.         g_pSound->Reset();
  280.     }
  281.  
  282.     // Display the OpenFileName dialog. Then, try to load the specified file
  283.     if( TRUE != GetOpenFileName( &ofn ) )
  284.     {
  285.         if( g_pSound )
  286.         {
  287.             EnableWindow( GetDlgItem( hDlg, IDC_PLAY ), TRUE );
  288.             EnableWindow( GetDlgItem( hDlg, IDC_STOP ), TRUE );
  289.         }
  290.  
  291.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("Load aborted.") );
  292.         return;
  293.     }
  294.  
  295.     SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  296.  
  297.     // Make sure wave file is a valid wav file
  298.     ValidateWaveFile( hDlg, strFileName );
  299.  
  300.     // Remember the path for next time
  301.     wcscpy( strPath, strFileName );
  302.     WCHAR* strLastSlash = wcsrchr( strPath, L'\\' );
  303.     if( strLastSlash )
  304.         strLastSlash[0] = '\0';
  305. }
  306.  
  307.  
  308.  
  309.  
  310. //-----------------------------------------------------------------------------
  311. // Name: ValidateWaveFile()
  312. // Desc: Open the wave file with the helper 
  313. //       class CWaveFile to make sure it is valid
  314. //-----------------------------------------------------------------------------
  315. VOID ValidateWaveFile( HWND hDlg, TCHAR* strFileName )
  316. {
  317.     HRESULT hr;
  318.  
  319.     CWaveFile waveFile;
  320.  
  321.     // Verify the file is small
  322.     HANDLE hFile = CreateFile( strFileName, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
  323.     if( hFile != NULL )
  324.     {
  325.         // If you try to open a 100MB wav file, you could run out of system memory with this
  326.         // sample cause it puts all of it into a large buffer.  If you need to do this, then 
  327.         // see the "StreamData" sample to stream the data from the file into a sound buffer.
  328.         DWORD dwFileSizeHigh = 0;
  329.         DWORD dwFileSize = GetFileSize( hFile, &dwFileSizeHigh );
  330.         CloseHandle( hFile );
  331.  
  332.         if( dwFileSizeHigh != 0 || dwFileSize > 1000000 )
  333.         {
  334.             waveFile.Close();
  335.             SetDlgItemText( hDlg, IDC_STATUS, TEXT("File too large.  You should stream large files.") );
  336.             return;
  337.         }
  338.     }
  339.  
  340.     // Load the wave file
  341.     if( FAILED( hr = waveFile.Open( strFileName, NULL, WAVEFILE_READ ) ) )
  342.     {
  343.         DXTRACE_ERR_MSGBOX( TEXT("Open"), hr );
  344.         waveFile.Close();
  345.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("Bad wave file.") );
  346.     }
  347.     else // The load call succeeded
  348.     {
  349.         // Update the UI controls to show the sound as the file is loaded
  350.         waveFile.Close();
  351.  
  352.         EnablePlayUI( hDlg, TRUE );
  353.         SetDlgItemText( hDlg, IDC_FILENAME, strFileName );
  354.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("File loaded.") );
  355.         wcscpy( m_strWaveFileName, strFileName );
  356.  
  357.         // Get the samples per sec from the wave file
  358.         DWORD dwSamplesPerSec = waveFile.m_pwfx->nSamplesPerSec;
  359.  
  360.         // Set the slider positions
  361.         SetSlidersPos( hDlg, dwSamplesPerSec, 0, 0 );
  362.     }
  363. }
  364.  
  365.  
  366.  
  367.  
  368. //-----------------------------------------------------------------------------
  369. // Name: OnPlaySound()
  370. // Desc: User hit the "Play" button
  371. //-----------------------------------------------------------------------------
  372. HRESULT OnPlaySound( HWND hDlg ) 
  373. {
  374.     HRESULT hr;
  375.     DWORD dwCreationFlags;
  376.  
  377.     BOOL bLooped      = ( IsDlgButtonChecked( hDlg, IDC_LOOP_CHECK )      == BST_CHECKED );
  378.     BOOL bFocusSticky = ( IsDlgButtonChecked( hDlg, IDC_FOCUS_STICKY )    == BST_CHECKED );
  379.     BOOL bFocusGlobal = ( IsDlgButtonChecked( hDlg, IDC_FOCUS_GLOBAL )    == BST_CHECKED );
  380.     BOOL bMixHardware = ( IsDlgButtonChecked( hDlg, IDC_MIX_HARDWARE ) == BST_CHECKED );
  381.     BOOL bMixSoftware = ( IsDlgButtonChecked( hDlg, IDC_MIX_SOFTWARE ) == BST_CHECKED );
  382.  
  383.     // Detrimine the creation flags to use based on the radio buttons
  384.     dwCreationFlags = 0;
  385.     if( bFocusGlobal )
  386.         dwCreationFlags |= DSBCAPS_GLOBALFOCUS;
  387.  
  388.     if( bFocusSticky )
  389.         dwCreationFlags |= DSBCAPS_STICKYFOCUS;
  390.  
  391.     if( bMixHardware )
  392.         dwCreationFlags |= DSBCAPS_LOCHARDWARE;
  393.  
  394.     if( bMixSoftware )
  395.         dwCreationFlags |= DSBCAPS_LOCSOFTWARE;
  396.  
  397.     // Add extra flags needed for the UI
  398.     dwCreationFlags |= DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;
  399.  
  400.     // Free any previous sound
  401.     SAFE_DELETE( g_pSound );
  402.  
  403.     // Since the user can change the focus before the sound is played, 
  404.     // we need to create the sound buffer every time the play button is pressed 
  405.  
  406.     // Load the wave file into a DirectSound buffer
  407.     if( FAILED( hr = g_pSoundManager->Create( &g_pSound, m_strWaveFileName, dwCreationFlags, GUID_NULL ) ) )
  408.     {
  409.         // Not a critical failure, so just update the status
  410.         DXTRACE_ERR( TEXT("Create"), hr );
  411.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("Could not create sound buffer.") );
  412.         return S_FALSE; 
  413.     }
  414.  
  415.     // Set the buffer options to what the sliders are set to
  416.     OnSliderChanged( hDlg );
  417.  
  418.     // Only if the sound buffer was created perfectly should we update the UI 
  419.     // and play the sound
  420.  
  421.     // Get the position of the sliders
  422.     HWND hFreqSlider   = GetDlgItem( hDlg, IDC_FREQUENCY_SLIDER );
  423.     HWND hPanSlider    = GetDlgItem( hDlg, IDC_PAN_SLIDER );
  424.     HWND hVolumeSlider = GetDlgItem( hDlg, IDC_VOLUME_SLIDER );
  425.     LONG lFrequency = (LONG)SendMessage( hFreqSlider,   TBM_GETPOS, 0, 0 ) * 1L;
  426.     LONG lPan       = (LONG)SendMessage( hPanSlider,    TBM_GETPOS, 0, 0 ) * 500L;
  427.     LONG lVolume    = (LONG)SendMessage( hVolumeSlider, TBM_GETPOS, 0, 0 ) * 100L;
  428.  
  429.     // Play the sound
  430.     DWORD dwLooped = bLooped ? DSBPLAY_LOOPING : 0L;
  431.  
  432.     if( FAILED( hr = g_pSound->Play( 0, dwLooped, lVolume, lFrequency, lPan ) ) )
  433.         return DXTRACE_ERR( TEXT("Play"), hr );
  434.  
  435.     // Update the UI controls to show the sound as playing
  436.     EnablePlayUI( hDlg, FALSE );
  437.     SetDlgItemText( hDlg, IDC_STATUS, TEXT("Sound playing.") );
  438.  
  439.     return S_OK;
  440. }
  441.  
  442.  
  443.  
  444.  
  445. //-----------------------------------------------------------------------------
  446. // Name: OnTimer()
  447. // Desc: When we think the sound is playing this periodically checks to see if 
  448. //       the sound has stopped.  If it has then updates the dialog.
  449. //-----------------------------------------------------------------------------
  450. VOID OnTimer( HWND hDlg ) 
  451. {
  452.     if( IsWindowEnabled( GetDlgItem( hDlg, IDC_STOP ) ) )
  453.     {
  454.         // We think the sound is playing, so see if it has stopped yet.
  455.         if( !g_pSound->IsSoundPlaying() ) 
  456.         {
  457.             // Update the UI controls to show the sound as stopped
  458.             EnablePlayUI( hDlg, TRUE );
  459.             SetDlgItemText( hDlg, IDC_STATUS, TEXT("Sound stopped.") );
  460.         }
  461.     }
  462. }
  463.  
  464.  
  465.  
  466.  
  467. //-----------------------------------------------------------------------------
  468. // Name: OnSliderChanged()  
  469. // Desc: Called when the dialog's slider bars are changed by the user, or need
  470. //       updating
  471. //-----------------------------------------------------------------------------
  472. VOID OnSliderChanged( HWND hDlg )
  473. {
  474.     TCHAR strBuffer[10];
  475.  
  476.     // Get handles to dialog items
  477.     HWND hFreqSlider   = GetDlgItem( hDlg, IDC_FREQUENCY_SLIDER );
  478.     HWND hPanSlider    = GetDlgItem( hDlg, IDC_PAN_SLIDER );
  479.     HWND hVolumeSlider = GetDlgItem( hDlg, IDC_VOLUME_SLIDER );
  480.  
  481.     // Get the position of the sliders
  482.     LONG lFrequency = (LONG)SendMessage( hFreqSlider,   TBM_GETPOS, 0, 0 ) * 1L;
  483.     LONG lPan       = (LONG)SendMessage( hPanSlider,    TBM_GETPOS, 0, 0 ) * 500L;
  484.     LONG lVolume    = (LONG)SendMessage( hVolumeSlider, TBM_GETPOS, 0, 0 ) * 100L;
  485.  
  486.     // Set the static text boxes
  487.     wsprintf( strBuffer, TEXT("%ld"), lFrequency );
  488.     SetWindowText( GetDlgItem( hDlg, IDC_FREQUENCY ), strBuffer );
  489.  
  490.     wsprintf( strBuffer, TEXT("%ld"), lPan );
  491.     SetWindowText( GetDlgItem( hDlg, IDC_PAN       ), strBuffer );
  492.  
  493.     wsprintf( strBuffer, TEXT("%ld"), lVolume );
  494.     SetWindowText( GetDlgItem( hDlg, IDC_VOLUME    ), strBuffer );
  495.  
  496.     // Set the options in the DirectSound buffer
  497.     if( g_pSound )
  498.     {
  499.         LPDIRECTSOUNDBUFFER pDSB = g_pSound->GetBuffer( 0 );
  500.  
  501.         if( pDSB )
  502.         {
  503.             if( FAILED( pDSB->SetFrequency( lFrequency ) ) )
  504.             {
  505.                 DSCAPS dscaps;
  506.                 ZeroMemory( &dscaps, sizeof(DSCAPS) );
  507.                 dscaps.dwSize = sizeof(DSCAPS);
  508.                 g_pSoundManager->GetDirectSound()->GetCaps( &dscaps );
  509.                 
  510.                 DSBCAPS dsbcaps;
  511.                 ZeroMemory( &dsbcaps, sizeof(DSBCAPS) );
  512.                 dsbcaps.dwSize = sizeof(DSBCAPS);
  513.                 pDSB->GetCaps( &dsbcaps );
  514.  
  515.                 // Try to guess why it failed 
  516.                 if( (dsbcaps.dwFlags & DSBCAPS_LOCHARDWARE) && 
  517.                     (DWORD)lFrequency > dscaps.dwMaxSecondarySampleRate )
  518.                 {                    
  519.                     // Hardware buffers don't support >dwMaxSecondarySampleRate
  520.                     SetDlgItemText( hDlg, IDC_STATUS, TEXT("Hardware buffers don't support greater") 
  521.                                                       TEXT("than dscaps.dwMaxSecondarySampleRate") );
  522.                 }
  523.                 else if( lFrequency > 100000 )
  524.                 {
  525.                     // Some platforms (pre-WinXP SP1) do not support 
  526.                     // >100k Hz so they will fail when setting it higher
  527.                     SetDlgItemText( hDlg, IDC_STATUS, TEXT("Some OS platforms do not support >100k Hz") );
  528.                 }
  529.                 else
  530.                 {
  531.                     SetDlgItemText( hDlg, IDC_STATUS, TEXT("Set frequency failed") );
  532.                 }
  533.  
  534.                 // Reset to the last valid freq
  535.                 pDSB->SetFrequency( g_dwLastValidFreq );                  
  536.                 PostMessage( hFreqSlider, TBM_SETPOS, TRUE, g_dwLastValidFreq );               
  537.             }
  538.             else
  539.             {
  540.                 g_dwLastValidFreq = lFrequency;
  541.             }
  542.             
  543.             pDSB->SetPan( lPan );
  544.             pDSB->SetVolume( lVolume );
  545.         }
  546.     }
  547. }
  548.  
  549.  
  550.  
  551.  
  552. //-----------------------------------------------------------------------------
  553. // Name: UpdateBehaviorText()
  554. // Desc: Figure out what the expected behavoir is based on the dialog,
  555. //       and display it on the dialog
  556. //-----------------------------------------------------------------------------
  557. VOID UpdateBehaviorText( HWND hDlg )
  558. {
  559.     TCHAR strExcepted[1024];
  560.  
  561.     BOOL bFocusSticky = ( IsDlgButtonChecked( hDlg, IDC_FOCUS_STICKY ) == BST_CHECKED );
  562.     BOOL bFocusGlobal = ( IsDlgButtonChecked( hDlg, IDC_FOCUS_GLOBAL ) == BST_CHECKED );
  563.     BOOL bMixHardware = ( IsDlgButtonChecked( hDlg, IDC_MIX_HARDWARE ) == BST_CHECKED );
  564.     BOOL bMixSoftware = ( IsDlgButtonChecked( hDlg, IDC_MIX_SOFTWARE ) == BST_CHECKED );
  565.  
  566.     // Figure what the user should expect based on the dialog choice
  567.     if( bFocusSticky )
  568.     {
  569.         wcscpy( strExcepted, L"Buffers with \"sticky\" focus will continue to play "
  570.                              L"if the user switches to another application not using "
  571.                              L"DirectSound.  However, if the user switches to another "
  572.                              L"DirectSound application, all normal-focus and sticky-focus "
  573.                              L"buffers in the previous application are muted." ); 
  574.  
  575.     }
  576.     else if( bFocusGlobal )
  577.     {
  578.         wcscpy( strExcepted, L"Buffers with global focus will continue to play if the user "
  579.                              L"switches focus to another application, even if the new application "
  580.                              L"uses DirectSound. The one exception is if you switch focus to a "
  581.                              L"DirectSound application that uses the DSSCL_WRITEPRIMARY "
  582.                              L"cooperative level. In this case, the global-focus buffers from "
  583.                              L"other applications will not be audible." );
  584.     }
  585.     else
  586.     {
  587.         // Normal focus
  588.         wcscpy( strExcepted, L"Buffers with normal focus will mute if the user switches "
  589.                              L"focus to any other application" );
  590.     }
  591.  
  592.  
  593.     if( bMixHardware )
  594.     {
  595.         wcscat( strExcepted, L"\n\nWith the hardware mixing flag, the new buffer will "
  596.                              L"be forced to use hardware mixing. If the device does "
  597.                              L"not support hardware mixing or if the required "
  598.                              L"hardware resources are not available, the call to the "
  599.                              L"IDirectSound::CreateSoundBuffer method will fail." ); 
  600.     }
  601.     else if( bMixSoftware )
  602.     {
  603.         wcscat( strExcepted, L"\n\nWith the software mixing flag, the new buffer will use "
  604.                              L"software mixing, even if hardware resources are available." );
  605.     }
  606.     else 
  607.     {
  608.         // Default mixing
  609.         wcscat( strExcepted, L"\n\nWith default mixing, the new buffer will use hardware "
  610.                              L"mixing if available, otherwise software mixing will be used." ); 
  611.     }
  612.  
  613.     // Tell the user what to expect
  614.     SetDlgItemText( hDlg, IDC_BEHAVIOR, strExcepted );
  615. }
  616.  
  617.  
  618.  
  619.  
  620. //-----------------------------------------------------------------------------
  621. // Name: EnablePlayUI()
  622. // Desc: Enables or disables the Play UI controls 
  623. //-----------------------------------------------------------------------------
  624. VOID EnablePlayUI( HWND hDlg, BOOL bEnable )
  625. {
  626.     if( bEnable )
  627.     {
  628.         EnableWindow( GetDlgItem( hDlg, IDC_LOOP_CHECK      ), TRUE );
  629.         EnableWindow( GetDlgItem( hDlg, IDC_PLAY            ), TRUE );
  630.         EnableWindow( GetDlgItem( hDlg, IDC_STOP            ), FALSE );
  631.  
  632.         EnableWindow( GetDlgItem( hDlg, IDC_FOCUS_NORMAL    ), TRUE );
  633.         EnableWindow( GetDlgItem( hDlg, IDC_FOCUS_STICKY    ), TRUE );
  634.         EnableWindow( GetDlgItem( hDlg, IDC_FOCUS_GLOBAL    ), TRUE );
  635.  
  636.         EnableWindow( GetDlgItem( hDlg, IDC_MIX_DEFAULT  ), TRUE );
  637.         EnableWindow( GetDlgItem( hDlg, IDC_MIX_HARDWARE ), TRUE );
  638.         EnableWindow( GetDlgItem( hDlg, IDC_MIX_SOFTWARE ), TRUE );
  639.  
  640.         SetFocus(     GetDlgItem( hDlg, IDC_PLAY ) );
  641.     }
  642.     else
  643.     {
  644.         EnableWindow( GetDlgItem( hDlg, IDC_LOOP_CHECK      ), FALSE );
  645.         EnableWindow( GetDlgItem( hDlg, IDC_PLAY            ), FALSE );
  646.         EnableWindow( GetDlgItem( hDlg, IDC_STOP            ), TRUE );
  647.  
  648.         EnableWindow( GetDlgItem( hDlg, IDC_FOCUS_NORMAL    ), FALSE );
  649.         EnableWindow( GetDlgItem( hDlg, IDC_FOCUS_STICKY    ), FALSE );
  650.         EnableWindow( GetDlgItem( hDlg, IDC_FOCUS_GLOBAL    ), FALSE );
  651.  
  652.         EnableWindow( GetDlgItem( hDlg, IDC_MIX_DEFAULT  ), FALSE );
  653.         EnableWindow( GetDlgItem( hDlg, IDC_MIX_HARDWARE ), FALSE );
  654.         EnableWindow( GetDlgItem( hDlg, IDC_MIX_SOFTWARE ), FALSE );
  655.  
  656.         SetFocus(     GetDlgItem( hDlg, IDC_STOP ) );
  657.     }
  658. }
  659.  
  660.  
  661.  
  662.  
  663. //-----------------------------------------------------------------------------
  664. // Name: SetSlidersPos()
  665. // Desc: Sets the slider positions
  666. //-----------------------------------------------------------------------------
  667. VOID SetSlidersPos( HWND hDlg, LONG lFreqSlider, LONG lPanSlider, LONG lVolumeSlider )
  668. {
  669.     HWND hFreqSlider    = GetDlgItem( hDlg, IDC_FREQUENCY_SLIDER );
  670.     HWND hPanSlider     = GetDlgItem( hDlg, IDC_PAN_SLIDER );
  671.     HWND hVolumeSlider  = GetDlgItem( hDlg, IDC_VOLUME_SLIDER );
  672.  
  673.     PostMessage( hFreqSlider,   TBM_SETPOS, TRUE, lFreqSlider );
  674.     PostMessage( hPanSlider,    TBM_SETPOS, TRUE, lPanSlider );
  675.     PostMessage( hVolumeSlider, TBM_SETPOS, TRUE, lVolumeSlider );
  676. }
  677.  
  678.  
  679.  
  680.